import { get, post, put } from '../../common/ajax';
import { BRD_SIGNIN_URL, USR_SIGNIN_URL } from '../../constants/url';
import { dealAwardRecordListData} from '../../biztools/record';
import { dealSigninSaveData } from '../biztools/signin';

// 品牌侧

/**
 * 获取品牌签到信息
 */
 export const getSigninDetail = async () => {
    const res = await get(BRD_SIGNIN_URL.detail(), null, false);
    return res.data || {};
};


/**
 * 编辑品牌签到信息
 */
 export const editSignin = async (params) => {
    params = dealSigninSaveData(params);
    const res = await put(BRD_SIGNIN_URL.update(), params);
    return res.data || false;
};

/**
 * 关闭品牌签到信息
 * @param {Object} params activityId
 */
export const pauseSignin = async (params) => {
    const res = await put(BRD_SIGNIN_URL.pause(), params);
    return res.data || false;
};

// 用户侧

/**
 * 获取品牌签到信息
 * @param {String|Number} brandId 
 */
export const getSigninInfo = async (brandId) => {
    const res = await get(USR_SIGNIN_URL.signinInfo(brandId));
    const { def = {}, procedure = {}, stat = {} } = res.data || {};
    if (!procedure.isContinuous) {
        procedure.continuousCheckInCount = 0;
    }
    if (procedure.isCheckInToday && procedure.continuousCheckInCount === 0 && def.typeInfo) {
        procedure.continuousCheckInCount = def.typeInfo.continuous;
    }
    return {
        signinInfo: def,
        procedureInfo: procedure,
        statInfo: stat,
    };
};

/**
 * 签到
 * @param {Object} params
 */
export const doSignin = async (params) => {
    const res = await post(USR_SIGNIN_URL.signinDo, params);
    return res.data || false;
};

/**
 * 获取中奖记录列表
 * @param {Number} pageSize 每页数据
 * @param {Number} pageNo 页数
 */
 export const getAwawrdList = async (params) => {
    params.pageSize = params.pageSize || 10;
    params.awardType = 'real_prize';
    const res = await get(USR_SIGNIN_URL.signinAwards, params);
    const { total = 0, rows = [] } = res.data || {};
    return { total, rows: dealAwardRecordListData(rows) };
};
